package com.jar.exercise.网络编程.UDP连接.demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

class Sender2 {
	public static void main(String[] args) {
		try {
			String data = "这个时我数据";
			DatagramSocket socket = new DatagramSocket();
			byte[] bytes = data.getBytes();
			InetAddress ip = InetAddress.getByName("127.0.0.1");
			DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, ip, 12000);
			socket.send(packet);
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class Server2 {
	public static void main(String[] args) throws IOException {
		DatagramSocket socket = new DatagramSocket(12000);
		byte[] b = new byte[1024];
		DatagramPacket packet = new DatagramPacket(b, 0, b.length);
		socket.receive(packet);
		byte[] data = packet.getData();
		System.out.println(new String(data, 0, packet.getLength()));
	}
}
